<?php
require_once 'libs/top/Taoapi.php';
require_once 'include/config.inc.php';
require_once 'global.func.php';
class TopWrap
{
	private $_topApi;
	private $_memcache;
	private $_cache;
	private $_runmsg;
	private $_nick;
	public function __construct($topApiConfig=false, $cache=false)
	{
		$this->_cache = $cache;	
		$this->_topApi = new Taoapi();
		$this->_nick   = $this->_topApi->ApiConfig->nick;
		$this->_runmsg = array();
		if( $this->_cache ) $this->initMemcached();
	}
	
	private function docachedata( $key, &$data )
	{
		if( !isset($data['error_rsp']) )
		{
			if($this->_cache)$this->_setData($key, $data);
		}
	}
	
	public function getItemCats($id='0')
	{
		$this->_topApi->method = 'taobao.itemcats.get';
		$this->_topApi->fields = 'cid,parent_cid,name,is_parent,status,sort_order';
		$this->_topApi->setVersion(2);
		$this->_topApi->parent_cid=$id;
		$key = $this->_topApi->method.$this->_topApi->parent_cid;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			//array_push($this->_runmsg, 'get cached data');
			if ( $data && !empty($data) ) return $data;
		}
		//array_push($this->_runmsg, 'get uncached data');
		$cat_items = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $cat_items);
		return $cat_items;
	}
	
	public function getItemCat($cid)
	{
		$this->_topApi->method = 'taobao.itemcats.get';
		$this->_topApi->fields = 'cid,parent_cid,name,is_parent,status,sort_order';
		$this->_topApi->setVersion(2);
		$this->_topApi->cids=$cid;
		$key = $this->_topApi->method.'cat'.$this->_topApi->cids;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			//array_push($this->_runmsg, 'get cached data');
			if ( $data && !empty($data) ) return $data;
		}
		//array_push($this->_runmsg, 'get uncached data');
		$cat_items = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $cat_items);
		return $cat_items;
	}
	
	public function getItems($cid,$page_no=1,$page_size=15,$sort='credit_desc')
	{
		$this->_topApi->method = 'taobao.taobaoke.items.get';
		$this->_topApi->fields = 'iid,num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume';
		$this->_topApi->setVersion(2);
		$this->_topApi->cid = $cid;
		$this->_topApi->nick = $this->_nick;
		$this->_topApi->page_no = $page_no;
		$this->_topApi->page_size=$page_size;
		$this->_topApi->sort = $sort;
		$key = $this->_topApi->method.$this->_topApi->cid.$page_no.$page_size.$sort;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			//array_push($this->_runmsg, 'get cached data');
			if ( $data && !empty($data) ) return $data;
		}
		//array_push($this->_runmsg, 'get uncached data');
		$cat_items = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $cat_items);
		return $cat_items;		
	}
	
	public function searchItems( $cid, $q, $page_no=1, $page_size=15, $sort='commissionRate_desc' )
	{
		$this->_topApi->method = 'taobao.taobaoke.items.get';
		$this->_topApi->fields = 'iid,num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume';
		$this->_topApi->setVersion(2);
		$this->_topApi->cid = $cid;
		$this->_topApi->nick = $this->_nick;
		$this->_topApi->page_no = $page_no;
		$this->_topApi->page_size=$page_size;
		$this->_topApi->sort = $sort;
		$this->_topApi->keyword = $q;
		$cat_items = $this->_topApi->Send()->getArrayData();
		return $cat_items;			
	}
	
	public function getItemByIID($iid)
	{
		$this->_topApi->method = 'taobao.taobaoke.items.detail.get';
		$this->_topApi->fields = 'iid,detail_url,num_iid,title,nick,type,cid,seller_cids,pic_url,input_pids,input_str,desc,pic_path,num,valid_thru,list_time,delist_time,stuff_status,location,price,post_fee,express_fee,ems_fee,has_discount,freight_payer,has_invoice,has_warranty,has_showcase,modified,increment,auto_repost,approve_status,postage_id,product_id,auction_point,itemimg,propimg,sku,outer_id,is_virtural,is_taobao,is_ex,video,click_url,shop_click_url,seller_credit_score';
		$this->_topApi->setVersion(2);
		$this->_topApi->num_iids = $iid;
		$this->_topApi->nick = $this->_nick;
		$key = $this->_topApi->method.$this->_topApi->num_iids;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			if ( $data && !empty($data) ) return $data;
		}
		$cat_items = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $cat_items);
		return $cat_items;		
	}
	
	public function shopConvert($sids)
	{
		$this->_topApi->method = 'taobao.taobaoke.shops.convert';
		$this->_topApi->fields = 'user_id,shop_title,click_url,commission_rate';
		$this->_topApi->setVersion(2);
		$this->_topApi->nick = $this->_nick;
		$this->_topApi->sids = $sids;
		$key = 'shopconvert_'.$sids;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			if( $data && !empty($data) )return $data;
		}
		$data = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $data);
		return $data;
	}
	
	public function itemConvert($iid,$nick)
	{
		$this->_topApi->method = 'taobao.taobaoke.items.convert';
		$this->_topApi->fields = 'iid,num_iid,title,nick,pic_url,price,click_url,commission,shop_click_url';
		$this->_topApi->setVersion(2);
		$this->_topApi->nick = $nick;
		$this->_topApi->iids = $iid;
		$key = 'itemconvert_'.$iid;
		if($this->_cache)
		{
			$data = $this->_getData($key);
			if( $data && !empty($data) )return $data;
		}
		$data = $this->_topApi->Send()->getArrayData();
		$this->docachedata($key, $data);
		return $data;
	}
	
	public function getMsg()
	{
		return $this->_runmsg;
	}
	
	public function clearMemcached() {
		return $this->_clear();
	}
	
	private function initMemcached()
	{
		$this->_memcache = new Memcache();
		$this->_memcache->addserver('127.0.0.1', 11211);
	}

	private function _setData($key, $val, $expire=3600)
	{
		return $this->_memcache->set($key, $val, false, $expire);
	}
	
	private function _getData($key)
	{
		return $this->_memcache->get($key);
	}
	
	private function _rm($key) {
		return $this->_memcache->delete($key);
	}
	private function _clear() {
		return $this->_memcache->flush();
	}
}

function gen_nav($cid, &$taoapi)
{
	$str = '';
	if($cid == '0') return '';
	$cat = $taoapi->getItemCat($cid);
	if(empty($cat))return $str;
	if(isset($cat['item_cats']['item_cat']['parent_cid']) == false) return $str;
	
	if( $cat['item_cats']['item_cat']['parent_cid'] != '0' )
	{
		$str .= gen_nav($cat['item_cats']['item_cat']['parent_cid'],&$taoapi);
	}
	$str .= "-> <a href=\"".url('list',array('catid'=>$cat['item_cats']['item_cat']['cid']))."\">".$cat['item_cats']['item_cat']['name'].'</a>';
	return $str;
}